package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.DeviceAgeHistory;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface DeviceAgeHistoryDao {

    @Results(id = "deviceAgeHistoryResultMap", value = {
            @Result(column = "device_age_history_id", property = "deviceAgeHistoryId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_id", property = "pcbaDeviceId", jdbcType = JdbcType.INTEGER),
            @Result(column = "type", property = "type", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "audit_status", property = "auditStatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "close_remark", property = "closeRemark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "close_files", property = "closeFiles", jdbcType = JdbcType.VARCHAR),
            @Result(column = "close_audit_reason", property = "closeAuditReason", jdbcType = JdbcType.VARCHAR),
            @Result(column = "submitter_id", property = "submitterId", jdbcType = JdbcType.INTEGER),
            @Result(column = "auditor_id", property = "auditorId", jdbcType = JdbcType.INTEGER),
            @Result(column = "close_method", property = "closeMethod", jdbcType = JdbcType.INTEGER),
            @Result(column = "close_uuid", property = "closeUuid", jdbcType = JdbcType.VARCHAR),
    })
    @Select("select * from " + DB1TableName.DEVICE_AGE_HISTORY + " where type = #{type} and pcba_device_id = #{pcbaDeviceId} order by create_time desc")
    List<DeviceAgeHistory> getDeviceAgeHistoryByDeviceId(@Param("pcbaDeviceId")Integer pcbaDeviceId, @Param("type")Integer type);

    @Insert("insert into " + DB1TableName.DEVICE_AGE_HISTORY + " (pcba_device_id,type,create_time,audit_status,close_remark,close_files,close_audit_reason,submitter_id,auditor_id,close_method,close_uuid) values " +
            "(#{deviceAgeHistory.pcbaDeviceId},#{deviceAgeHistory.type},#{deviceAgeHistory.createTime},#{deviceAgeHistory.auditStatus},#{deviceAgeHistory.closeRemark},#{deviceAgeHistory.closeFiles},#{deviceAgeHistory.closeAuditReason},#{deviceAgeHistory.submitterId},#{deviceAgeHistory.auditorId},#{deviceAgeHistory.closeMethod},#{deviceAgeHistory.closeUuid})")
    Integer addDeviceAgeHistory(@Param("deviceAgeHistory")DeviceAgeHistory deviceAgeHistory);

    /**
     * 按照设备id 流程uuid type 查询
     */
    @ResultMap(value = "deviceAgeHistoryResultMap")
    @Select("select * from " + DB1TableName.DEVICE_AGE_HISTORY + " where type = #{type} and pcba_device_id = #{pcbaDeviceId} and close_uuid = #{closeUuid} order by create_time desc")
    List<DeviceAgeHistory> getDeviceAgeHistoryByDeviceIdAndUuidAndType(@Param("pcbaDeviceId")Integer pcbaDeviceId, @Param("type")Integer type, @Param("closeUuid")String closeUuid);

    /**
     * 更新
     */
    @Update("<script>" +
            "update " + DB1TableName.DEVICE_AGE_HISTORY +
            "<set>" +
            "<if test='deviceAgeHistory.closeAuditReason != null and deviceAgeHistory.closeAuditReason != \"\" '>" +
            " close_audit_reason = #{deviceAgeHistory.closeAuditReason}," +
            "</if>" +
            "<if test='deviceAgeHistory.auditStatus != null and deviceAgeHistory.auditStatus != \"\" '>" +
            " audit_status = #{deviceAgeHistory.auditStatus}," +
            "</if>" +
            "<if test='deviceAgeHistory.auditorId != null and deviceAgeHistory.auditorId != \"\" '>" +
            " auditor_id = #{deviceAgeHistory.auditorId}," +
            "</if>" +
            "<if test='deviceAgeHistory.updateTime != null '>" +
            " update_time = #{deviceAgeHistory.updateTime}," +
            "</if>" +
            "</set>" +
            " where device_age_history_id = #{deviceAgeHistory.deviceAgeHistoryId}" +
            "</script>")
    Integer updateDeviceAgeHistory(@Param("deviceAgeHistory")DeviceAgeHistory deviceAgeHistory);

    /**
     * 根据类型 + uuid查询
     */
    @ResultMap(value="deviceAgeHistoryResultMap")
    @Select("select * from " + DB1TableName.DEVICE_AGE_HISTORY + " where type = #{type} and close_uuid = #{closeUuid}")
    List<DeviceAgeHistory> getByTypeAndUuid(@Param("type")Integer type,@Param("closeUuid")String closeUuid);
}
